package com.jy.adscm.zltx.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.system.service.JyUserService;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.JyDrawingIssueRecovery;
import com.jy.adscm.zltx.bean.JyDrawingList;
import com.jy.adscm.zltx.service.JyDrawingEditInfoService;
import com.jy.adscm.zltx.service.JyDrawingIssueRecoveryService;
import com.jy.adscm.zltx.service.JyDrawingListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 图纸的回收与发放记录表 前端控制器
 * </p>
 *
 * @author yyy
 * @since 2018-11-08
 */
@RestController
@RequestMapping("/adscm/zltx/jyDrawingIssueRecovery")
public class JyDrawingIssueRecoveryController {
    @Autowired
    JyDrawingListService jyDrawingListService;
    @Autowired
    JyDrawingEditInfoService jyDrawingEditInfoService;
    @Autowired
    JyDrawingIssueRecoveryService jyDrawingIssueRecoveryService;
    @Autowired
    JyUserService jyUserService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    /**
     * 新增发放记录
     * @param jyDrawingIssueRecovery
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/insert")
    public SimpleCoreResponse insert(@RequestBody JyDrawingIssueRecovery jyDrawingIssueRecovery, HttpServletRequest request) throws AdscmSystemException {

        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        JyUser user = token.getUser();

        jyDrawingIssueRecovery.setIssuer(user.getId());
        jyDrawingIssueRecovery.setReceiveTime(new Date());

        Long drawingId = jyDrawingIssueRecovery.getDrawingId();//图纸ID
        JyDrawingList jyDrawingList = jyDrawingListService.selectById(drawingId);

        jyDrawingIssueRecovery.setDrawingVersion(jyDrawingList.getDrawingVersion());
        jyDrawingListService.updateJyDrawing(Integer.valueOf(String.valueOf(jyDrawingIssueRecovery.getDrawingId())));
        jyDrawingIssueRecoveryService.insert(jyDrawingIssueRecovery);

        return new SimpleCoreResponse("200","新增成功" ,null);
    }
    /**
     * 回收
     * @param jyDrawingIssueRecovery
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/recoveryDrawing")
    public SimpleCoreResponse recoveryDrawing(@RequestBody JyDrawingIssueRecovery jyDrawingIssueRecovery, HttpServletRequest request) throws AdscmSystemException {

        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        JyUser user = token.getUser();

        jyDrawingIssueRecovery.setRecoverier(user.getId());
        jyDrawingIssueRecovery.setRecoveryTime(new Date());
        jyDrawingIssueRecoveryService.updateById(jyDrawingIssueRecovery);

        return new SimpleCoreResponse("200","回收成功" ,null);
    }
    /**
     * 查询回收与发放记录
     * @param drawingId
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/selectIssueRecovery/{drawingId}")
    public SimpleCoreResponse selectIssueRecovery(@PathVariable Integer drawingId) throws AdscmSystemException {

        Wrapper<JyDrawingIssueRecovery> wrapper = new EntityWrapper<>();
        wrapper.eq("drawing_id",drawingId);
        List<JyDrawingIssueRecovery> recoveries = jyDrawingIssueRecoveryService.selectList(wrapper);
        if (recoveries != null && recoveries.size()>0){
            for (JyDrawingIssueRecovery recovery : recoveries) {
                Integer receiver = recovery.getReceiver();
                Integer recoverier = recovery.getRecoverier();
                Integer issuer = recovery.getIssuer();

                if (receiver != null){
                    JyUser user = jyUserService.selectById(receiver);
                    recovery.setReceiverName(user.getName());
                }

                if (recoverier != null){
                    JyUser user = jyUserService.selectById(recoverier);
                    recovery.setRecoverierName(user.getName());
                }

                if (issuer != null){
                    JyUser user = jyUserService.selectById(issuer);
                    recovery.setIssuerName(user.getName());
                }
            }
        }
        return new SimpleCoreResponse("200","查询成功" ,recoveries);
    }














}

